<template>
  <div class="container">
    <div class="ibox-content no-border no-padding-bottom">
      <!--搜索栏-->
      <el-col class="toolbar">
        <el-form :inline="true" :model="filterForm" size="mini" label-position="top" ref="filterForm">
          <el-form-item label="id">
            <el-input v-model="filterForm.id" placeholder="id"></el-input>
          </el-form-item>
          <el-form-item label="减息卡类型">
            <el-select style="width: 178px" v-model="filterForm.card_type_id" placeholder="减息卡类型">
              <el-option label="全部" value=""></el-option>
              <el-option :label="item" :value="parseInt(key)"
                         v-for="(item,key,index) in selectOptions.card_type" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="减息卡名称">
            <el-input v-model="filterForm.card_name" placeholder="减息卡名称"></el-input>
          </el-form-item>
          <el-form-item label="卡金额">
            <el-input type="number" :min="1" v-model="filterForm.card_amount" placeholder="卡金额"></el-input>
          </el-form-item>
          <el-form-item label="匹配次数">
            <el-input type="number" :min="1" v-model="filterForm.loan_number" placeholder="匹配次数"></el-input>
          </el-form-item>
          <el-form-item label="状态">
            <el-select style="width: 178px" v-model="filterForm.status" placeholder="状态">
              <el-option label="全部" value=""></el-option>
              <el-option :label="item" :value="parseInt(key)"
                         v-for="(item,key,index) in selectOptions.status" :key="index"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="margin-top: 28px">
            <el-button @click="filterClick" type="primary">查询</el-button>
            <el-button @click="resetFilterForm('filterForm')">重置</el-button>
          </el-form-item>
          <div class="hr-line-dashed"></div>
          <br>
          <el-form-item size="small">
            <el-button @click="handleAdd" type="primary" plain icon="el-icon-plus">添加减息卡配置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>

    <div class="ibox-content">
      <!--表格-->
      <el-table
        ref="multipleTable"
        :data="arrList"
        size="medium"
        border
        highlight-current-row
        tooltip-effect="dark"
        style="width: 100%"
        :max-height="500"
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="card_type_id" label="卡类型">
          <template slot-scope="scope">
            <span v-if="scope.row.card_type_id==1">首贷</span>
            <span v-if="scope.row.card_type_id==2">复贷</span>
            <span v-if="scope.row.card_type_id==10">首还</span>
            <span v-if="scope.row.card_type_id==20">复还</span>
          </template>
        </el-table-column>
        <el-table-column prop="card_name" label="卡名称"></el-table-column>
        <el-table-column prop="card_amount" label="卡金额">
          <template slot-scope="scope">
            <span>￥{{scope.row.card_amount}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="loan_number" label="匹配次数">
          <template slot-scope="scope">
            <span>{{scope.row.loan_number}}次</span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <span class="green" v-if="scope.row.status==10">启用</span>
            <span class="red" v-if="scope.row.status==20">禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="100" fixed="right">
          <template slot-scope="scope">
            <el-tooltip @click.native="changeStatus(scope.row,1)" v-if="scope.row.status==10" :enterable="false"
                        effect="dark" content="设为禁用"
                        placement="top-start">
              <el-button type="danger" plain size="mini" icon="el-icon-lock"></el-button>
            </el-tooltip>
            <el-tooltip @click.native="changeStatus(scope.row,2)" v-if="scope.row.status==20" :enterable="false"
                        effect="dark" content="设为启用"
                        placement="top-start">
              <el-button type="primary" plain size="mini" icon="el-icon-unlock"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页工具条-->
      <div v-show="arrList.length>0" style="margin-top: 18px;text-align: center">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageInfo.currPage"
          :page-sizes="[20]"
          :page-size="pageInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.totalCount">
        </el-pagination>
      </div>

    </div>

    <!--    修改/添加-->
    <el-dialog width="26%" :title="editFormTitle" :visible.sync="editFormVisible" @close="closeDialog"
               :close-on-click-modal="false">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" size="small">
        <el-form-item label="卡名称" prop="card_name">
          <el-input v-model="editForm.card_name"></el-input>
        </el-form-item>
        <el-form-item label="卡类型" prop="card_type_id">
          <br>
          <el-select style="width: 100%" v-model="editForm.card_type_id" placeholder="减息卡类型">
            <el-option label="全部" value=""></el-option>
            <el-option :label="item" :value="parseInt(key)"
                       v-for="(item,key,index) in selectOptions.card_type" :key="index"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="卡金额" prop="card_amount">
          <el-input type="number" :min="1" v-model="editForm.card_amount"></el-input>
        </el-form-item>
        <el-form-item label="匹配次数" prop="loan_number">
          <el-input type="number" :min="1" v-model="editForm.loan_number"></el-input>
        </el-form-item>
        <el-form-item label="用户分类" prop="status">
          <br>
          <el-radio-group v-model="editForm.card_category">
            <el-radio :label="parseInt(key)" v-for="(item,key,index) in selectOptions.card_category" :key="index">
              {{item}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <br>
          <el-radio-group v-model="editForm.status">
            <el-radio :label="parseInt(key)" v-for="(item,key,index) in selectOptions.status" :key="index">{{item}}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click.native="closeDialog">取消</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit('editForm')">提交</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    cashbackcardlist,
    changeCashbackcardStatus,
    cashbackcardOptions,
    addCashbackcard
  } from "../../api/cash-back-card";

  export default {
    name: "RateCardSetting",
    data() {
      return {
        editFormVisible: false,
        isAdd: true,
        editFormTitle: '',
        editForm: {
          status: 10,
          card_category: 1
        },
        editFormRules: {
          card_name: [{required: true, message: '不能为空'}],
          card_type_id: [{required: true, message: '不能为空'}],
          card_amount: [{required: true, message: '不能为空'}],
          loan_number: [{required: true, message: '不能为空'}],
          card_category: [{required: true, message: '不能为空'}],
          status: [{required: true, message: '不能为空'}],
        },
        arrList: [],
        pageInfo: {
          totalPage: 0,// 总页数
          totalCount: 0,// 总条数
          pageSize: 0,// 每页条数
          currPage: 1// 当前页
        },
        filterForm: {},
        tableLoading: false,
        tableSelectData: [],
        selectOptions: {},
        chooseTime: ''
      }
    },
    created() {
      this.getOptions();
      this.initData();
    },
    methods: {
      initData() {
        this.tableLoading = true;
        cashbackcardlist({
          page: this.pageInfo.currPage,
          ...this.filterForm
        }).then(res => {
          this.tableLoading = false;
          const {list, page} = res.data;
          this.arrList = list;
          this.pageInfo = page
        }).catch(() => {
          this.tableLoading = false
        })
      },
      getOptions() {
        cashbackcardOptions({}).then(res => {
          this.selectOptions = res.data
        }).catch(error=>{

        })
      },
      //分页
      handleCurrentChange(val) {
        // console.log(val);
        this.pageInfo.currPage = val;
        this.initData();
      },

      //勾选
      handleSelectionChange(data) {
        this.tableSelectData = data;
      },
      filterClick() {
        this.pageInfo.currPage = 1;
        this.initData();
      },
      //重置
      resetFilterForm(form) {
        this.$refs[form].resetFields();
        this.filterForm = {};
        this.pageInfo.currPage = 1;
        this.initData();
      },
      closeDialog() {
        this.editFormVisible = false;
        this.filterForm = {
          status: 10,
          card_category: 1
        };
        this.resetForm();
      },
      editSubmit(form) {
        this.$refs[form].validate(valida => {
          if (valida) {
            addCashbackcard({
              ...this.editForm
            }).then(res => {
              this.editFormVisible = false;
              this.$message.success(res.msg);
              this.initData();
              this.editForm = {
                status: 10,
                card_category: 1
              }
            }).catch(error => {

            })
          }
        });
      },
      //清楚form 验证
      resetForm() {
        this.$refs['editForm'].clearValidate();
      },

      handleAdd() {
        this.editFormTitle = '添加减息卡配置';
        this.editFormVisible = true;
        this.isAdd = true;
      },

      changeStatus(row, type) {
        if (type == 1) {
          this.$confirm(`确定要禁用减息卡${row.card_name}吗`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(() => {
            changeCashbackcardStatus({id: row.id}).then(res => {
              this.$message.success(res.msg);
              this.initData()
            }).catch(()=>{})
          }).catch(() => {

          })
        } else {
          this.$confirm(`确定要启用减息卡${row.card_name}吗`, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
          }).then(() => {
            changeCashbackcardStatus({id: row.id}).then(res => {
              this.$message.success(res.msg);
              this.initData()
            }).catch(error=>{})

          }).catch(() => {

          })
        }

      }
    }
  }
</script>

<style scoped lang="scss">

</style>
